{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# FFI 张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pytest\n",
    "\n",
    "try:\n",
    "    import torch\n",
    "except ImportError:\n",
    "    torch = None\n",
    "\n",
    "from tvm import ffi as tvm_ffi\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 张量属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.zeros((10, 8, 4, 2), dtype=\"int16\")\n",
    "if not hasattr(data, \"__dlpack__\"):\n",
    "    raise \n",
    "x = tvm_ffi.from_dlpack(data)\n",
    "assert isinstance(x, tvm_ffi.NDArray)\n",
    "assert x.shape == (10, 8, 4, 2)\n",
    "assert x.dtype == tvm_ffi.dtype(\"int16\")\n",
    "assert x.device.device_type == tvm_ffi.Device.kDLCPU\n",
    "assert x.device.device_id == 0\n",
    "x2 = np.from_dlpack(x)\n",
    "np.testing.assert_equal(x2, data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 张量形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "shape = tvm_ffi.Shape((10, 8, 4, 2))\n",
    "assert isinstance(shape, tvm_ffi.Shape)\n",
    "assert shape == (10, 8, 4, 2)\n",
    "\n",
    "fecho = tvm_ffi.convert(lambda x: x)\n",
    "shape2 = fecho(shape)\n",
    "assert shape2.__tvm_ffi_object__.same_as(shape.__tvm_ffi_object__)\n",
    "assert isinstance(shape2, tvm_ffi.Shape)\n",
    "assert isinstance(shape2, tuple)\n",
    "\n",
    "shape3 = tvm_ffi.convert(shape)\n",
    "assert shape3.__tvm_ffi_object__.same_as(shape.__tvm_ffi_object__)\n",
    "assert isinstance(shape3, tvm_ffi.Shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## auto_dlpack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def check(x, y):\n",
    "    assert isinstance(y, tvm_ffi.NDArray)\n",
    "    assert y.shape == (128,)\n",
    "    assert y.dtype == tvm_ffi.dtype(\"int64\")\n",
    "    assert y.device.device_type == tvm_ffi.Device.kDLCPU\n",
    "    assert y.device.device_id == 0\n",
    "    x2 = torch.from_dlpack(y)\n",
    "    np.testing.assert_equal(x2.numpy(), x.numpy())\n",
    "\n",
    "x = torch.arange(128)\n",
    "fecho = tvm_ffi.get_global_func(\"testing.echo\")\n",
    "y = fecho(x)\n",
    "check(x, y)\n",
    "\n",
    "# pass in list of tensors\n",
    "y = fecho([x])\n",
    "check(x, y[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ai",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
